package com.zjsru.plan2024.normal;

import com.zjsru.leetcode75.level1.tree.TreeNode;

import java.util.List;

/**
 * @Author: cookLee
 * @Date: 2024-01-29
 * 相同的树
 */
public class IsSameTree {

    /**
     * 主
     * \
     * 输入：p = [1,2,3], q = [1,2,3]
     * 输出：true
     * \
     * 输入：p = [1,2], q = [1,null,2]
     * 输出：false
     * \
     * @param args args
     */
    public static void main(String[] args) {
        IsSameTree isSameTree = new IsSameTree();
        TreeNode p = new TreeNode(1, new TreeNode(2), new TreeNode(3));
        TreeNode q = new TreeNode(1, new TreeNode(2), new TreeNode(3));
        System.out.println(isSameTree.isSameTree(p, q));
    }

    /**
     * 是同一棵树:调用中序遍历的方法
     *
     * @param p p
     * @param q q
     * @return boolean
     */
    public boolean isSameTree(TreeNode p, TreeNode q) {
        InorderTraversal inorderTraversal = new InorderTraversal();
        List<Integer> pList = inorderTraversal.inorderTraversal(p);
        List<Integer> qList = inorderTraversal.inorderTraversal(q);
        return pList.equals(qList);
    }

    /**
     * 深度遍历
     *
     * @param p p
     * @param q q
     * @return boolean
     */
    public boolean isSameTree2(TreeNode p, TreeNode q) {
        if (p == null && q == null) {
            return true;
        } else if (p == null || q == null) {
            return false;
        }else if (p.val != q.val) {
            return false;
        } else {
            return this.isSameTree(p.left, q.left) && this.isSameTree(p.right, q.right);
        }
    }
}
